
package com.securecrt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSchException;

import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class terminalTelnet extends Activity{

	private TextView tv = null;
	private EditText et = null;
	AutomatedTelnetClient conn;
	Channel channel;
	Button btnGo, btnClear;
	String  host, port;

	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.terminal);
        
        tv = (TextView) findViewById(R.id.text_view);
        et = (EditText) findViewById(R.id.edit_view);
       
    	host = getIntent().getExtras().getString("server");
    	port = getIntent().getExtras().getString("port");
    	
    	    	
		try {
			conn = new AutomatedTelnetClient(host, Integer.parseInt(port),"","");
		}  catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

    	//tv.setText("["+host+"@"+"]$ ");
    	
        et.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View view, int keyCode, KeyEvent event){
              if (event.getAction() == KeyEvent.ACTION_DOWN){
                switch (keyCode)
                {
                  case KeyEvent.KEYCODE_DPAD_CENTER:
                  case KeyEvent.KEYCODE_ENTER:
                	    String command = et.getText().toString();	                	
						try {
							
			                String result = conn.sendCommand(command);
			                tv.setText("["+host+"@"+"]# " + command + "\n" +result);
						}catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

                    return true;
                  default:
                    break;
                }
              }
              return false;
            }
          });
        
        btnGo=(Button)findViewById(R.id.btnGo);
        btnGo.setOnClickListener(new OnClickListener() {
    		
    		@Override
    		public void onClick(View v) {
    			// TODO Auto-generated method stub);
    			String command = et.getText().toString();
    			try {
    				
	                String result = conn.sendCommand(command);
	                tv.setText("["+host+"@"+"]# " + command + "\n" +result);
    			}catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    		}
        });
        
        btnClear=(Button)findViewById(R.id.btnClear);
        btnClear.setOnClickListener(new OnClickListener() {
    		
    		@Override
    		public void onClick(View v) {
    			// TODO Auto-generated method stub);
    			tv.setText("");
    			conn.disconnect();
    			ExitApplication();
    		}
        });		
    }

    private void ExitApplication()
    {
    		this.finish();
    }
	
    public String Process(String command)
    {
    	Process process = null;
        try {
            // Executes the command.
        	//String command = "ls";
        	if(command.length() < 1) {
    			System.out.println("No arguments");
    			System.exit(1);
    		}
            process = Runtime.getRuntime().exec(command);
         
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            BufferedReader stdError = new BufferedReader(new 
                    InputStreamReader(process.getErrorStream()));

            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            // read any errors from the attempted command
            String s;
            while ((s = stdError.readLine()) != null) {
                   Toast.makeText(this, "Any errors from the attempted command !"+s, Toast.LENGTH_LONG).show();
                  
            }      
            stdError.close();
            reader.close();

            // Waits for the command to finish.
            process.waitFor();
            
            return output.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally{
        	process.destroy();
        }
    }


    
}
